package com.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GridTestServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public GridTestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		// 定义返回的数据类型：json，使用了json-lib  
		        JSONObject jsonObj = new JSONObject();  
		          
		        // 根据jqGrid对JSON的数据格式要求给jsonObj赋值  
		        jsonObj.put("page", 1);     // 当前页  
		        jsonObj.put("total", 1);    // 总页数  
		        jsonObj.put("records", 4);  // 总记录数  
		          
		        // 定义rows，存放数据  
		        JSONArray rows = new JSONArray();  
		        // 放入4条数据  
		        for(int i=0;i<4;i++)  
		        {  
		            // 存放一条记录的对象  
		            JSONObject cell = new JSONObject();  
		            cell.put("id", i);  
		            if(i%2==0)  
		            {  
		                cell.put("userName", "polaris");  
		                cell.put("gender", "女");  
		            }  
		            else  
		            {  
		                cell.put("userName", "徐新华");  
		                cell.put("gender", "男");  
		            }  
		            cell.put("email", "polaris@gmail.com");  
		            cell.put("QQ", "772"+i+"1837"+i);  
		            cell.put("mobilePhone", "132"+i+"1837"+i+"3"+i);  
		            cell.put("birthday", "198"+i+"-10-"+"1"+i);  
		              
		            // 将该记录放入rows中  
		            rows.add(cell);  
		        }  
		          
		        // 将rows放入json对象中  
		        jsonObj.put("rows", rows);  
		          
		        // 自控制台打印输出，以检验json对象生成是否正确  
		        System.out.println("要返回的json对象：\n" + jsonObj.toString());  
		          
		        // 设置字符编码  
		        response.setCharacterEncoding("UTF-8");  
		        // 返回json对象（通过PrintWriter输出）  
		        response.getWriter().print(jsonObj);  

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
